package client;



import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.TimeUnit;

/***
 * @author: SJun
 * @Date: 2020/8/13 9:42
 * @description:
 *
 */
public class ClientHandle extends SimpleChannelInboundHandler {
    static int[] status = {0x04, 0xff, 0x10,0x20,0x30,0x40};
    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) {

    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        byte[] data = ByteBufUtil.ByteBufToBytes(msg);
        int d = ByteHexUtil.byteToHex(data[0]);
        ByteBuf byteBuf = null;
        switch (d) {
            case 0x00:
                // 回复设备号
                int[] receive = {0x03, d, 0x01};
                byteBuf=ByteBufUtil.hexToByteBuf(receive);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(10);
                            // 有人
                            int[] done = {0x02, 0x31};
                            ctx.channel().writeAndFlush(ByteBufUtil.hexToByteBuf(done));
                            int[] _status = {0x04, 0xff, 0x10,0x20,0x31,0x40};
                            status = _status;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 有人
                        try {
                            TimeUnit.SECONDS.sleep(10);
                            int[] done = {0x02, 0x21};
                            ctx.channel().writeAndFlush(ByteBufUtil.hexToByteBuf(done));
                            int[] _status = {0x04, 0xff, 0x10,0x21,0x31,0x40};
                            status = _status;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 无人
                        try {
                            TimeUnit.SECONDS.sleep(10);
                            int[] done = {0x02, 0x20};
                            ctx.channel().writeAndFlush(ByteBufUtil.hexToByteBuf(done));
                            int[] _status ={0x04, 0xff, 0x10,0x20,0x31,0x40};
                            status = _status;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 无人
                        try {
                            TimeUnit.SECONDS.sleep(10);
                            int[] done = {0x02, 0x30};
                            ctx.channel().writeAndFlush(ByteBufUtil.hexToByteBuf(done));
                            int[] _status = {0x04, 0xff, 0x10,0x20,0x30,0x40};
                            status = _status;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // 有人
                        try {
                            TimeUnit.SECONDS.sleep(30);
                            int[] done = {0x02, 0x21};
                            ctx.channel().writeAndFlush(ByteBufUtil.hexToByteBuf(done));
                            int[] _status = {0x04, 0xff, 0x10,0x21,0x30,0x40};
                            status = _status;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case 0xff:
                // 回复设备状态
                byteBuf=ByteBufUtil.hexToByteBuf(status);
            case 0x11:
                // 回复设备状态
                int[] _status = {0x04, d, 0x11,0x31};
                status = _status;
                int[] done = {0x03, d, 0x00};
                byteBuf=ByteBufUtil.hexToByteBuf(done);
            case 0x10:
                // 回复设备状态
                int[] __status = {0x04, d, 0x10,0x31};
                status = __status;
                int[] _done = {0x03, d, 0x00};
                byteBuf=ByteBufUtil.hexToByteBuf(_done);


        }
        ctx.channel().writeAndFlush(byteBuf);


        System.out.println("Client received: " + ByteHexUtil.byteToHex(data[0]));


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause) {
        cause.printStackTrace();
        channelHandlerContext.close();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        super.channelInactive(ctx);

    }


}
